/*
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
 *
 * Copyright (c) 2013-2014 sagyf Yang. The Four Group.
 */

$(document).ready(function () {

    document.addEventListener('WeixinJSBridgeReady', function onBridgeReady() {
        WeixinJSBridge.call('hideToolbar');
    });
    var template = Handlebars.compile($("#entry-template").html());
    var tpl_comment = Handlebars.compile($("#comment-template").html());

    var $commentlist = $('#comment_list');

    $.get(g.ctx + 'mobileweb/mallActivity/comments/' + g.activity, function (rst) {
        if (rst.status == 'OK') {
            var data = rst.data;
            $commentlist.html(tpl_comment(data));
        } else {
            showMesage('加载评论信息出现错误')
        }
    });

    var showMesage = function (text) {
        noty({
            text        : text,
            type        : 'warning',
            dismissQueue: true,
            timeout     : 1000,
            layout      : 'topCenter',
            theme       : 'defaultTheme',
            maxVisible  : 10
        })
    };

    $("#sb").click(function () {
        var val = $.trim($("#area-cnt").val());
        if (val == "") {
            showMesage("请输入评论内容!");
            return;
        }
        val = val.replace(/<[^>].*?>/g, ""); //去除html标签
        $.post(
            g.ctx + "mobileweb/mallActivity/comment",
            {mid: g.member, aid: g.activity, comment: val},
            function (data) {
                if (data.status == "OK") {
                    var $commentItemList = $('#comment_item_list');
                    if($commentItemList){
                        var appHtml = template(data.data);
                        $commentItemList.append(appHtml);
                    } else {
                        $commentlist.html(tpl_comment([data.data]))
                    }

                    showMesage(data.message);
                    $("#area-cnt").val("");
                    $('html, body, .content').animate({scrollTop: $(document).height()}, 300);
                }
                else {
                    showMesage(data.message);
                }
            }
        )
    });


    $("#btn-act,#btn-act-middle").on("click", function () {
        location.href = "${ctx}/mobileweb/mallActivity/enrollPage?aid=" + g.activity + "&mid=" + g.member;
    });
});